package com.ruoyi.parking.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.parking.mapper.ParkingLotMapper;
import com.ruoyi.parking.mapper.ParkingSpaceMapper;
import com.ruoyi.parking.domain.ParkingLot;
import com.ruoyi.parking.service.IParkingLotService;

/**
 * 停车场管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@Service
public class ParkingLotServiceImpl implements IParkingLotService 
{
    @Autowired
    private ParkingLotMapper parkingLotMapper;

    @Autowired
    private ParkingSpaceMapper parkingSpaceMapper;

    /**
     * 查询停车场管理
     * 
     * @param id 停车场管理主键
     * @return 停车场管理
     */
    @Override
    public ParkingLot selectParkingLotById(Long id)
    {
        return parkingLotMapper.selectParkingLotById(id);
    }

    /**
     * 查询停车场管理列表
     * 
     * @param parkingLot 停车场管理
     * @return 停车场管理
     */
    @Override
    public List<ParkingLot> selectParkingLotList(ParkingLot parkingLot)
    {
        return parkingLotMapper.selectParkingLotList(parkingLot);
    }

    /**
     * 新增停车场管理
     * 
     * @param parkingLot 停车场管理
     * @return 结果
     */
    @Override
    public int insertParkingLot(ParkingLot parkingLot)
    {
        return parkingLotMapper.insertParkingLot(parkingLot);
    }

    /**
     * 修改停车场管理
     * 
     * @param parkingLot 停车场管理
     * @return 结果
     */
    @Override
    public int updateParkingLot(ParkingLot parkingLot)
    {
        return parkingLotMapper.updateParkingLot(parkingLot);
    }

    /**
     * 批量删除停车场管理
     * 
     * @param ids 需要删除的停车场管理主键
     * @return 结果
     */
    @Override
    public int deleteParkingLotByIds(Long[] ids)
    {
        // 检查是否有车位关联
        for (Long id : ids)
        {
            int spaceCount = parkingSpaceMapper.countByLotAndStatus(id, null);
            if (spaceCount > 0)
            {
                ParkingLot lot = parkingLotMapper.selectParkingLotById(id);
                throw new ServiceException("停车场【" + (lot != null ? lot.getLotName() : id) + "】下还有" + spaceCount + "个车位，无法删除");
            }
        }
        return parkingLotMapper.deleteParkingLotByIds(ids);
    }

    /**
     * 删除停车场管理信息
     * 
     * @param id 停车场管理主键
     * @return 结果
     */
    @Override
    public int deleteParkingLotById(Long id)
    {
        // 检查是否有车位关联
        int spaceCount = parkingSpaceMapper.countByLotAndStatus(id, null);
        if (spaceCount > 0)
        {
            ParkingLot lot = parkingLotMapper.selectParkingLotById(id);
            throw new ServiceException("停车场【" + (lot != null ? lot.getLotName() : id) + "】下还有" + spaceCount + "个车位，无法删除");
        }
        return parkingLotMapper.deleteParkingLotById(id);
    }
}
